﻿function removeContent(target, height) {
    var c = target.text().length;
    if (target.height() > height) {
        while (target.height() > 32 && c > 0) {
            c--;
            var html = toSubString(target.html(), c);
            target.html(html);
        }
        target.html(toSubString(target.html(), target.html().length - 3) + "...");
    }
}

function toSubString(p, length) {
    if (p == '' || p.Length <= length)
        return p;
    var indexOfSpace = p.lastIndexOf(' ');
    p = p.substring(0, Math.min(p.length, length));
    if (p.length > indexOfSpace)
        p = p.substring(0, indexOfSpace);
    return p;
}

String.format = function (text) {
    if (arguments.length <= 1) {
        return text;
    }

    var tokenCount = arguments.length - 2;
    for (var token = 0; token <= tokenCount; token++) {
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"),
                                                arguments[token + 1]);
    }
    return text;
};

String.prototype.trimStart = function (c) {
    if (this.length == 0)
        return this;
    c = c ? c : ' ';
    var i = 0;
    for (; this.charAt(i) == c && i < this.length; i++);
    return this.substring(i);
};

String.prototype.trimEnd = function (c) {
    c = c ? c : ' ';
    var i = this.length - 1;
    for (; i >= 0 && this.charAt(i) == c; i--);
    return this.substring(0, i + 1);
};

String.prototype.trim = function (c) {
    return this.trimStart(c).trimEnd(c);
};


var keyStr = "ABCDEFGHIJKLMNOP" +
               "QRSTUVWXYZabcdef" +
               "ghijklmnopqrstuv" +
               "wxyz0123456789+/" +
               "=";

function encode64(input) {
    
    input = escape(input);
    var output = "";
    var chr1, chr2, chr3 = "";
    var enc1, enc2, enc3, enc4 = "";
    var i = 0;

    do {
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);

        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;

        if (isNaN(chr2)) {
            enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
            enc4 = 64;
        }

        output = output +
           keyStr.charAt(enc1) +
           keyStr.charAt(enc2) +
           keyStr.charAt(enc3) +
           keyStr.charAt(enc4);
    } while (i < input.length);

    return output;
}


(function ($) {

    var keyString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    var uTF8Encode = function (string) {
        string = string.replace(/\x0d\x0a/g, "\x0a");
        var output = "";
        for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);
            if (c < 128) {
                output += String.fromCharCode(c);
            } else if ((c > 127) && (c < 2048)) {
                output += String.fromCharCode((c >> 6) | 192);
                output += String.fromCharCode((c & 63) | 128);
            } else {
                output += String.fromCharCode((c >> 12) | 224);
                output += String.fromCharCode(((c >> 6) & 63) | 128);
                output += String.fromCharCode((c & 63) | 128);
            }
        }
        return output;
    };

    var uTF8Decode = function (input) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while (i < input.length) {
            c = input.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            } else if ((c > 191) && (c < 224)) {
                c2 = input.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            } else {
                c2 = input.charCodeAt(i + 1);
                c3 = input.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    };
    $.extend({
        base64Encode: function (input) {
            var output = "";
            var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
            var i = 0;
            input = uTF8Encode(input);
            while (i < input.length) {
                chr1 = input.charCodeAt(i++);
                chr2 = input.charCodeAt(i++);
                chr3 = input.charCodeAt(i++);
                enc1 = chr1 >> 2;
                enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
                enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
                enc4 = chr3 & 63;
                if (isNaN(chr2)) {
                    enc3 = enc4 = 64;
                } else if (isNaN(chr3)) {
                    enc4 = 64;
                }
                output = output + keyString.charAt(enc1) + keyString.charAt(enc2) + keyString.charAt(enc3) + keyString.charAt(enc4);
            }
            return output;
        },
        base64Decode: function (input) {
            var output = "";
            var chr1, chr2, chr3;
            var enc1, enc2, enc3, enc4;
            var i = 0;
            input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
            while (i < input.length) {
                enc1 = keyString.indexOf(input.charAt(i++));
                enc2 = keyString.indexOf(input.charAt(i++));
                enc3 = keyString.indexOf(input.charAt(i++));
                enc4 = keyString.indexOf(input.charAt(i++));
                chr1 = (enc1 << 2) | (enc2 >> 4);
                chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
                chr3 = ((enc3 & 3) << 6) | enc4;
                output = output + String.fromCharCode(chr1);
                if (enc3 != 64) {
                    output = output + String.fromCharCode(chr2);
                }
                if (enc4 != 64) {
                    output = output + String.fromCharCode(chr3);
                }
            }
            output = uTF8Decode(output);
            return output;
        }
    });
})(jQuery);

/* Modified slimScroll plugin */
(function ($) {

    $.fn.slimScroll = function (o) {

        var ops = o;
        //do it for every element that matches selector
        this.each(function () {

            var isOverPanel, isOverBar, isDragg, queueHide, barHeight, divS = '<div></div>',
                minBarHeight = 30,
                wheelStep = 30,
                o = ops || {},
                cwidth = o.width || 'auto',
                cheight = o.height || '250px',
                size = o.size || '7px',
                color = o.color || '#000',
                position = o.position || 'right',
                opacity = o.opacity || .4,
                alwaysVisible = o.alwaysVisible === true;

            //used in event handlers and for better minification
            var me = $(this);

            //wrap content
            var wrapper = $(divS).css({
                position: 'relative',
                overflow: 'hidden',
                width: cwidth,
                height: cheight
            }).attr({
                'class': 'slimScrollDiv'
            });

            //update style for the div
            me.css({
                overflow: 'hidden',
                width: cwidth,
                height: cheight
            });

            //create scrollbar rail
            var rail = $(divS).css({
                width: '15px',
                height: '100%',
                position: 'absolute',
                top: 0
            });

            //create scrollbar
            var bar = $(divS).attr({
                'class': 'slimScrollBar ',
                style: 'border-radius: ' + size
            }).css({
                background: color,
                width: size,
                position: 'absolute',
                top: 0,
                opacity: opacity,
                display: alwaysVisible ? 'block' : 'none',
                BorderRadius: size,
                MozBorderRadius: size,
                WebkitBorderRadius: size,
                zIndex: 99
            });

            //set position
            var posCss = (position == 'right') ? {
                right: '1px'
            } : {
                left: '1px'
            };
            rail.css(posCss);
            bar.css(posCss);

            //wrap it
            me.wrap(wrapper);

            //append to parent div
            me.parent().append(bar);
            me.parent().append(rail);

            //make it draggable
            bar.draggable({
                axis: 'y',
                containment: 'parent',
                start: function () {
                    isDragg = true;
                },
                stop: function () {
                    isDragg = false;
                    hideBar();
                },
                drag: function (e) {
                    //scroll content
                    scrollContent(0, $(this).position().top, false);
                }
            });

            //on rail over
            rail.hover(function () {
                showBar();
            }, function () {
                hideBar();
            });

            //on bar over
            bar.hover(function () {
                isOverBar = true;
            }, function () {
                isOverBar = false;
            });

            //show on parent mouseover
            me.hover(function () {
                isOverPanel = true;
                showBar();
                hideBar();
            }, function () {
                isOverPanel = false;
                hideBar();
            });

            var _onWheel = function (e) {


                //use mouse wheel only when mouse is over
                if (!isOverPanel) {
                    return;
                }

                var e = e || window.event;

                var delta = 0;
                if (e.wheelDelta) {
                    delta = -e.wheelDelta / 120;
                }
                if (e.detail) {
                    delta = e.detail / 5;
                }

                //scroll content
                scrollContent(0, delta, true);

                //stop window scroll
                if (e.preventDefault) {
                    e.preventDefault();
                }
                e.returnValue = false;


                clearTimeout(queueHide);
                queueHide = setTimeout(function () {
                    hideBar();

                }, 1000);

            };
            me.bind('scrollContent', function (e, y) {
                scrollContent(0, y, true);
            });

            var scrollContent = function (x, y, isWheel) {

                //ensure bar is visible
                showBar();

                var delta = y;

                if (isWheel) {
                    //move bar with mouse wheel
                    delta = bar.position().top + y * wheelStep;

                    //move bar, make sure it doesn't go out
                    delta = Math.max(delta, 0);
                    var maxTop = me.outerHeight() - bar.outerHeight();
                    delta = Math.min(delta, maxTop);

                    //scroll the scrollbar
                    bar.css({
                        top: delta + 'px'
                    });

                }

                //calculate actual scroll amount
                var percentScroll = parseInt(bar.position().top) / (me.outerHeight() - bar.outerHeight());
                delta = percentScroll * (me[0].scrollHeight - me.outerHeight());

                //scroll content
                me.scrollTop(delta);

            }; /*
            test = setInterval(function()
            {
            showBar();

            delta = $("#inner").prop("scrollHeight");

            scrollContent(0, delta, true);


            }, 3000);
            */

            var attachWheel = function () {
                if (window.addEventListener) {
                    this.addEventListener('DOMMouseScroll', _onWheel, false);
                    this.addEventListener('mousewheel', _onWheel, false);
                }
                else {
                    document.attachEvent("onmousewheel", _onWheel);
                }
            }; //attach scroll events
            attachWheel();

            var getBarHeight = function () {
                //calculate scrollbar height and make sure it is not too small
                barHeight = Math.max((me.outerHeight() / me[0].scrollHeight) * me.outerHeight(), minBarHeight);
                bar.css({
                    height: barHeight + 'px'
                });
            }; //set up initial height
            getBarHeight();


            function define_delta(newdelta) {
                var delta = newdelta;
            }

            var showBar = function () {
                //recalculate bar height
                getBarHeight();
                clearTimeout(queueHide);

                //show only when required
                if (barHeight >= me.outerHeight()) {
                    return;
                }
                bar.fadeIn('fast');
            };
            var hideBar = function () {
                //only hide when options allow it
                if (!alwaysVisible) {
                    queueHide = setTimeout(function () {
                        if (!isOverBar && !isDragg) {
                            bar.fadeOut('fast');
                        }
                    }, 1000);
                }
            };
        });

        //maintain chainability
        return this;
    };
})(jQuery);

/**
* @name        jQuery FullScreen Plugin
* @author      Martin Angelov, Morten Sjøgren
* @version     1.1
* @url         http://tutorialzine.com/2012/02/enhance-your-website-fullscreen-api/
* @license     MIT License
*/

/*jshint browser: true, jquery: true */
(function ($) {
    "use strict";

    // These helper functions available only to our plugin scope.
    function supportFullScreen() {
        var doc = document.documentElement;

        return ('requestFullscreen' in doc) ||
				('mozRequestFullScreen' in doc && document.mozFullScreenEnabled) ||
				('webkitRequestFullScreen' in doc);
    }

    function requestFullScreen(elem) {
        if (elem.requestFullscreen) {
            elem.requestFullscreen();
        } else if (elem.mozRequestFullScreen) {
            elem.mozRequestFullScreen();
        } else if (elem.webkitRequestFullScreen) {
            elem.webkitRequestFullScreen();
        }
    }

    function fullScreenStatus() {
        return document.fullscreen ||
				document.mozFullScreen ||
				document.webkitIsFullScreen ||
				false;
    }

    function cancelFullScreen() {
        if (document.exitFullscreen) {
            document.exitFullscreen();
        } else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
        } else if (document.webkitCancelFullScreen) {
            document.webkitCancelFullScreen();
        }
        $(document).off('fullscreenchange mozfullscreenchange webkitfullscreenchange');
    }

    function onFullScreenEvent(callback) {
        $(document).on("fullscreenchange mozfullscreenchange webkitfullscreenchange", function () {
            // The full screen status is automatically
            // passed to our callback as an argument.
            callback(fullScreenStatus());
        });
    }

    // Adding a new test to the jQuery support object
    $.support.fullscreen = supportFullScreen();

    // Creating the plugin
    $.fn.fullScreen = function (props) {
        if (!$.support.fullscreen || this.length !== 1) {
            // The plugin can be called only
            // on one element at a time

            return this;
        }

        if (fullScreenStatus()) {
            // if we are already in fullscreen, exit
            cancelFullScreen();
            return this;
        }

        // You can potentially pas two arguments a color
        // for the background and a callback function

        var options = $.extend({
            'background': '#111',
            'callback': $.noop(),
            'fullscreenClass': 'fullScreen'
        }, props),

		elem = this,

        // This temporary div is the element that is
        // actually going to be enlarged in full screen

        //		fs = $('<div>', {
        //		    'css': {
        //		        'overflow-y': 'auto',
        //		        'background': options.background,
        //		        'width': '100%',
        //		        'height': '100%'
        //		    }
        //		})
        //			.insertBefore(elem)
        //			.append(elem);

        fs = $('#popup-detail-left');

        // You can use the .fullScreen class to
        // apply styling to your element
        elem.addClass(options.fullscreenClass);

        // Inserting our element in the temporary
        // div, after which we zoom it in fullscreen

        requestFullScreen(fs.get(0));

        //        fs.click(function (e) {
        //            if (e.target == this) {
        //                // If the black bar was clicked
        //                cancelFullScreen();
        //            }
        //        });

        elem.cancel = function () {
            cancelFullScreen();
            return elem;
        };

        onFullScreenEvent(function (fullScreen) {
            if (!fullScreen) {
                // We have exited full screen.
                // Remove the class and destroy
                // the temporary div

                elem.removeClass(options.fullscreenClass).insertBefore(fs);
                fs.remove();
            }

            // Calling the user supplied callback
            options.callback(fullScreen);
        });

        return elem;
    };

    $.fn.cancelFullScreen = function () {
        cancelFullScreen();

        return this;
    };
} (jQuery));

function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
    var c_value = document.cookie;
    var c_start = c_value.indexOf(" " + c_name + "=");
    if (c_start == -1) {
        c_start = c_value.indexOf(c_name + "=");
    }
    if (c_start == -1) {
        c_value = null;
    }
    else {
        c_start = c_value.indexOf("=", c_start) + 1;
        var c_end = c_value.indexOf(";", c_start);
        if (c_end == -1) {
            c_end = c_value.length;
        }
        c_value = unescape(c_value.substring(c_start, c_end));
    }
    return c_value;
}